package com.xcity.game.combat.builder;

import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.combat.Combat;
import com.xcity.game.combat.CombatBuilder;
import com.xcity.game.combat.CombatTeam;
import com.xcity.game.combat.listener.CombatListener;
import com.xcity.game.combat.unit.impl.EmployeeCombatUnit;
import com.xcity.game.combat.unit.impl.EmptyCombatUnit;
import com.xcity.game.combat.unit.impl.NpcCombatUnit;
import com.xcity.game.combat.unit.impl.PlayerCombatUnit;
import com.xcity.game.formation.Formation;
import com.xcity.game.npc.NpcTemplate;

/**
 * @deprecated
 * @see DefaultCombatBuilder
 * @author yang.li
 *
 */
@Deprecated
public class PVPCombatBuilder implements CombatBuilder {

	private Combat.Type type;
	private Player source;
	private long[] sourceEmployeeIds;
	private List<Employee> sourceEmployees;
	private Player target;
	private long[] targetEmployeeIds;
	private List<Employee> targetEmployees;
	private List<NpcTemplate> targetNpcs; // 目标玩家有可能为机器人
	private boolean targetIsNpc;
	private CombatListener listener;
	
	public PVPCombatBuilder() {}
	
	public PVPCombatBuilder setType(Combat.Type type) {
		this.type = type;
		return this;
	}
	
	public PVPCombatBuilder setSource(Player source) {
		this.source = source;
		return this;
	}
	
	public PVPCombatBuilder setSourceEmployeeIds(long[] sourceEmployees) {
		this.sourceEmployeeIds = sourceEmployees;
		return this;
	}
	
	public PVPCombatBuilder setSourceEmployees(List<Employee> sourceEmployees) {
		this.sourceEmployees = sourceEmployees;
		return this;
	}
	
	public PVPCombatBuilder setTarget(Player target) {
		this.target = target;
		return this;
	}
	
	public PVPCombatBuilder setTargetEmployeeIds(long[] targetEmployees) {
		this.targetEmployeeIds = targetEmployees;
		return this;
	}
	
	public PVPCombatBuilder setTargetEmployees(List<Employee> targetEmployees) {
		this.targetEmployees = targetEmployees;
		return this;
	}
	
	public PVPCombatBuilder setTargetNpcs(List<NpcTemplate> targetNpcs) {
		this.targetNpcs = targetNpcs;
		this.targetIsNpc = targetNpcs != null && !targetNpcs.isEmpty();
		return this;
	}
	
	public PVPCombatBuilder setListener(CombatListener listener) {
		this.listener = listener;
		return this;
	}
	
	public Combat build() {
		CombatTeam src = sourceEmployees != null ? buildTeam(source, sourceEmployees) : buildTeam(source, sourceEmployeeIds);
		CombatTeam tgt;
		if (targetIsNpc) {
			tgt = buildNpcTeam(target, targetNpcs);
		} else {
			tgt = targetEmployees != null ? buildTeam(target, targetEmployees) : buildTeam(target, targetEmployeeIds);
		}
		return new Combat(src, tgt, type, listener);
	}
	
	static CombatTeam buildTeam(Player player, long[] employees) {
		CombatTeam team = new CombatTeam();
		// add the leader in the first position
		PlayerCombatUnit leader = new PlayerCombatUnit(team, player);
		team.add(leader);
		// add the members
		if (employees == null) {
			employees = Formation.EMPTY_FORMATION;
		}
		for (long eid : employees) {
			Employee e = eid != 0L ? player.getEmployees().get(eid) : null;
			team.add(e != null ? new EmployeeCombatUnit(team, e) : EmptyCombatUnit.INSTANCE);
		}
		fillEmpty(team);
		return team;
	}
	
	static CombatTeam buildTeam(Player player, List<Employee> employees) {
		CombatTeam team = new CombatTeam();
		// add the leader in the first position
		PlayerCombatUnit leader = new PlayerCombatUnit(team, player);
		team.add(leader);
		// add the members
		for (int i = 0, size = employees != null ? employees.size() : 0; i < size; i++) {
			Employee e = employees.get(i);
			team.add(e != null ? new EmployeeCombatUnit(team, e) : EmptyCombatUnit.INSTANCE);
		}
		fillEmpty(team);
		return team;
	}
	
	static CombatTeam buildNpcTeam(Player player, List<NpcTemplate> npcs) {
		CombatTeam team = new CombatTeam();
		// add the leader in the first position
		PlayerCombatUnit leader = new PlayerCombatUnit(team, player);
		team.add(leader);
		// add the members
		for (int i = 0; i < npcs.size(); i++) {
			NpcTemplate npc = npcs.get(i);
			team.add(npc != null ? new NpcCombatUnit(team, npc) : EmptyCombatUnit.INSTANCE);
		}
		fillEmpty(team);
		return team;
	}
	
	// 填充剩余空位
	private static void fillEmpty(CombatTeam team) {
		int n = CombatTeam.DEFAULT_TEAM_CAPACITY - team.size();
		for (int i = 0; i < n; i++) {
			team.add(EmptyCombatUnit.INSTANCE);
		}
	}
	
}
